package com.mls.guardian.appst.chain.excutor;

import com.mls.guardian.appst.chain.TaskExecutor;
import com.mls.guardian.appst.chain.TaskInvoker;
import com.mls.guardian.appst.chain.TaskResult;
import com.mls.guardian.common.FileUtil;
import com.mls.guardian.dataobj.ModuleMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.List;

/**
 * Created by pengrenfang on 15/10/29.
 */
public class CopyWebExcutor implements TaskExecutor {
    private static final Logger log = LoggerFactory.getLogger(CopyWebExcutor.class);

    @Override
    public TaskResult exec(TaskInvoker invoker) {
        String targetFolder = invoker.getConfig().getCopyDest() + File.separator + invoker.getProject().getProjectName();

        TaskResult result = new TaskResult();
        try {
            log.info("----开始copy web----");
            List<String> appList = invoker.getProject().getWebModuleList();
            List<String> aliasList = invoker.getProject().getWebAliasList();
            if (invoker.getModuleList() != null && invoker.getModuleList().size() > 0) {
                //指定模块
                for (ModuleMessage item : invoker.getModuleList()) {
                    log.info("----copy web[{}]----", item.getModuleName());
                    int index = appList.indexOf(item.getModuleName());
                    String sourceFolder = invoker.getProject().getProjectPath() + File.separator + item.getModuleName() + File.separator + "build" + File.separator + "libs";
                    if (Files.notExists(Paths.get(sourceFolder))) {
                        //打包好了
                        String sourceFile = invoker.getProject().getProjectPath() + File.separator + item.getModuleName() + "-1.0.war";
                        if (Files.notExists(Paths.get(sourceFile))) {
                            //war文件不存在,可能已经解压了,复制解压之后的别名目录
                            sourceFolder = invoker.getProject().getProjectPath() + File.separator + aliasList.get(index);
                            targetFolder = invoker.getConfig().getCopyDest() + File.separator + invoker.getProject().getProjectName() + File.separator + aliasList.get(index);

                            FileUtil.copyFolder(sourceFolder, targetFolder, true);
                        } else {
                            //复制war文件
                            String targetFile = invoker.getConfig().getCopyDest() + File.separator + invoker.getProject().getProjectName() + File.separator + item.getModuleName() + "-1.0.war";
                            if (Files.notExists(Paths.get(targetFile).getParent())) {
                                Files.createDirectories(Paths.get(targetFile).getParent());
                            }
                            Files.copy(Paths.get(sourceFile), Paths.get(targetFile), StandardCopyOption.REPLACE_EXISTING);
                        }
                    } else {
                        FileUtil.copyFolder(sourceFolder, targetFolder, true);
                    }
                }
            } else {
                //所有模块
                for (int i = 0; i < invoker.getProject().getWebModuleList().size(); i++) {
                    String item = invoker.getProject().getWebModuleList().get(i);
                    log.info("----copy web[{}]----", item);

                    String sourceFolder = invoker.getProject().getProjectPath() + File.separator + item + File.separator + "build" + File.separator + "libs";
                    if (Files.notExists(Paths.get(sourceFolder))) {
                        //打包好了
                        String sourceFile = invoker.getProject().getProjectPath() + File.separator + item + "-1.0.war";
                        if (Files.notExists(Paths.get(sourceFile))) {
                            //war文件不存在,可能已经解压了,复制解压之后的别名目录
                            sourceFolder = invoker.getProject().getProjectPath() + File.separator + aliasList.get(i);
                            targetFolder = invoker.getConfig().getCopyDest() + File.separator + invoker.getProject().getProjectName() + File.separator + aliasList.get(i);

                            FileUtil.copyFolder(sourceFolder, targetFolder, true);
                        } else {
                            //复制war文件
                            String targetFile = invoker.getConfig().getCopyDest() + File.separator + invoker.getProject().getProjectName() + File.separator + item + "-1.0.war";
                            if (Files.notExists(Paths.get(targetFile).getParent())) {
                                Files.createDirectories(Paths.get(targetFile).getParent());
                            }
                            Files.copy(Paths.get(sourceFile), Paths.get(targetFile), StandardCopyOption.REPLACE_EXISTING);
                        }
                    } else {
                        FileUtil.copyFolder(sourceFolder, targetFolder, true);
                    }
                }
            }

            log.info("----copy web[{}]----", true);
            result.setSuc(true);
        } catch (IOException e) {
            log.info("----copy web[{}]----", false);
            result.setSuc(false);
            result.setError(e.getMessage());

            e.printStackTrace();
        } finally {
            log.info("----结束copy web----");
        }

        if (result.isSuc() == true) {
            return invoker.invoke();
        }

        return result;
    }
}